package com.tan.test;

import com.tan.config.MyConfig;
import com.tan.po.User;
import com.tan.service.IAccountService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@SpringJUnitConfig(classes = {MyConfig.class})
public class JdbcTemplateTest2 {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Test
    public void test2(){
        //使用模板插入数据
        jdbcTemplate.update("insert into t_user(name,age) values(?,?) ","赵六",22);
    }
    @Test
    public void queryForObjectTest(){
        String name=jdbcTemplate.queryForObject("select name from t_user where id=?",String.class,1);
        System.out.println(name);
    }
    @Test
    public void queryForObjectTest2(){
        String name=jdbcTemplate.queryForObject("select name from t_user where id=? and age>=?",String.class,1,15);
        System.out.println(name);
    }
    @Test
    public void queryForObjectTest3(){
        User user = jdbcTemplate.queryForObject("select * from t_user where id=?", new RowMapper<User>() {
            @Override
            public User mapRow(ResultSet resultSet, int i) throws SQLException {
                User user = new User();
                user.setId(resultSet.getInt("id"));
                user.setName(resultSet.getString("name"));
                user.setAge(resultSet.getInt("age"));
                return user;
            }
        }, 1);
        System.out.println(user);
    }
    @Test
    public void queryForObjectTest4(){
        User user=jdbcTemplate.queryForObject("select * from t_user where id=?",new BeanPropertyRowMapper<User>(User.class),1);
        System.out.println(user);
    }
    @Test
    public void queryForMapTest1(){
        Map<String, Object> maps = jdbcTemplate.queryForMap("select * from t_user where id=?",1);
        System.out.println(maps);

    }
    @Test
    public void queryForListTest1(){
        List<String> list = jdbcTemplate.queryForList("select name from t_user", String.class);
        System.out.println(list);
    }
    @Test
    public void queryForListTest2(){
        jdbcTemplate.queryForList("select * from t_user").forEach(System.out::println);
    }
    @Test
    public void queryTest(){
        List<User> query = jdbcTemplate.query("select * from t_user where id=?", new BeanPropertyRowMapper<User>(User.class), 1);
        System.out.println(query);
    }
    @Test
    public void batchUpdateTest(){
        List<Object[]> list=new ArrayList<Object[]>();
        for(int i=1;i<=3;i++){
            Object[] objs=new Object[2];
            objs[0]="张三"+i;
            objs[1]=23;
            list.add(objs);
        }
        jdbcTemplate.batchUpdate("insert into t_user(name,age) values (?,?)",list);
    }
    @Autowired
    private IAccountService accountService;
    @Test
    public void test(){
        accountService.transfer("小张","小王",100);
    }
}
